<template>
  <el-main class="bg table_wrap user">
    <!-- table头部检索 -->
    <div class="div-form">
      <el-form
        ref="elFormRef"
        :model="formInline"
        label-width="90px"
        :style="{ width: '100%' }"
        label-position="left"
      >
        <el-row :gutter="20">
          <el-col :span="5">
            <el-form-item label="昵称">
              <el-input v-model="formInline.nickname"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <baseTable
      :tableColumns="columns"
      :requestApi="url_get_list"
      :isShowSelect="true"
      :vm='vm'
      v-model:isRefresh="isRefresh"
      :otherParams="formInline"
      @handleSelectionChange="handleSelectionChange"
      @handleCurrentChangeNew="handleCurrentChangeNew"
    >
      <!-- 头部按钮 -->
      <template #baseHeadFunt>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button @click="onReset(true)">重置</el-button>
        <el-button
          type="danger"
          v-if="user_group == '管理员'"
          class="search_btn_del"
          @click="delInfo()"
          >删除</el-button
        >
        <!-- <el-button
          v-if="user_group == '管理员'"
          class="float-right search_btn_add"
          @click="router.push('./view?')"
          >添加</el-button
        > -->
      </template>
      <!-- table内操作 -->
      <template #baseTableFunt="{ scope }">
        <el-button
          type="primary"
          size="small"
          @click="router.push('./view?' + field + '=' + scope.row.user_id)"
          >详情</el-button
        >
      </template>
      <template #baseTableFunt2="{ scope }">
        <el-button
          type="primary"
          size="small"
          @click="router.push('./view?' + field + '=' + scope.user_id)"
          >详情</el-button
        >
      </template>
      <!-- 特殊表格内容 -->
      <template #tableContent="{ scope, column }" >
        <el-avatar
          v-if="column.prop === 'avatar'"
          :src="$fullUrl($banseUrl, scope.row.avatar)"
          style="width: 80px; height: 80px"
        >
          <img src="../../assets/images/user.png" />
        </el-avatar>

        <span v-if="column.prop === 'create_time'">{{
          formatDate(scope.row.create_time)
        }}</span>
        <span v-if="column.prop === 'login_time'">{{
          formatDate(scope.row.login_time)
        }}</span>
        <span v-if="column.prop === 'state'"> {{
          getObj(scope.row.state).text
        }}</span>
      </template>
      <template #tableContent2="{ scope, column }" >
        <!-- <span v-if="column.prop === 'avatar'"> {{ column.label }}： <el-avatar
        
        :src="$fullUrl($banseUrl, scope.avatar)"
        style="width: 80px; height: 80px"
      >
        <img src="../../../public/img/loginBg.png" />
      </el-avatar></span> -->
      <span v-if="column.prop === 'state'"> {{ column.label }}：{{
          getObj(scope.state).text
        }}</span>
        <span v-if="column.prop === 'create_time'"> {{ column.label }}：{{
          formatDate(scope.create_time)
        }}</span>
        <span v-if="column.prop === 'login_time'"> {{ column.label }}：{{
          formatDate(scope.login_time)
        }}</span>
      </template>
    </baseTable>
  </el-main>
</template>
<script setup>
import { useRouter } from "vue-router";
let router = useRouter();
import {
  $get_auth,
  func,
  paramsFun,
  $fullUrl,
  formatDate,
  $banseUrl,
  $check_action,
} from "@/utils/utils.js";
import { $post, $get } from "@/api/Api.js";
import { ElMessage } from "element-plus";
import { ref, toRefs, computed, onMounted } from "vue";
import storageHelper from "@/libs/StorageHelper";
let table = ref("user");
let field = ref("user_id");
let url_get_list = ref("/api/user/get_list");
let url_del = ref("/api/user/del?");
let user_id = storageHelper.getUserInfo().user_id;
let user_group = storageHelper.getGroupValue();
// 账号状态
let list_state = ref([
  {
    text: "可用",
    value: 1,
  },
  {
    text: "异常",
    value: 2,
  },
  {
    text: "已冻结",
    value: 3,
  },
  {
    text: "已注销",
    value: 4,
  },
]);
// 认证状态
let list_is_state = ref([
  {
    text: "未认证",
    value: 0,
  },
  {
    text: "认证中",
    value: 1,
  },
  {
    text: "已认证",
    value: 2,
  },
]);
let vm=ref({
    img: "avatar",
    id: "user_id",
})
// 列名
const columns = [
  { prop: "nickname", label: "名称" },
  { prop: "avatar", label: "头像" },
  { prop: "username", label: "用户名" },
  { prop: "user_group", label: "用户组" },
  { prop: "email", label: "邮箱" },
  { prop: "state", label: "状态" },
  { prop: "login_time", label: "上次登录时间" },
  { prop: "create_time", label: "创建时间" },
];
// 头部搜索// 搜索
let formInline = ref({
  user_group: "管理员",
  nickname: "",
});
// 刷新table
let isRefresh = ref(false);

//批量勾选数据
let selectList = ref([]);
//点击当前行数据
let currentRowTableProduct = ref({});
//表格勾选回调
let handleSelectionChange = (val) => {
  selectList.value = val.map((item) => {
    return item.user_id;
  });
};
//点击当前行
let handleCurrentChangeNew = (val) => {
  currentRowTableProduct.value = val;
};
// 批量删除
let delInfo = () => {
  ElMessageBox.confirm("此操作将永久删除该文件, 是否继续?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      selectList.value.map((item) => {
        delFun(item);
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        user: "取消操作",
      });
    });
};

// 调用删除方法
let delFun = (id) => {
  $get(url_del.value + "user_id=" + id).then((json) => {
    if (json.result) {
      ElMessage.success("删除成功");
    } else if (json.error) {
      ElMessage.error(json.error);
    }
    isRefresh.value = true;
  });
};

let table_class = ({ row, column, rowIndex, columnIndex }) => {
  return "table_class";
};

// 搜索
let onSubmit = () => {
  isRefresh.value = true;
};

// 重置
let onReset = (refresh) => {
  formInline.value = {
    nickname: "",
    user_group: "管理员",
  };
  isRefresh.value = refresh;
};
// 查询数组对象某个是否包含某个值,返回对应对象
let getObj = (val) => {
  let obj = list_state.value.find((item) => item.value == val);
  return obj;
};
onMounted(() => {});
</script>
<style lang="less" scoped>
.div-form {
  padding: 10px;
}
</style>
